﻿layui.use(['element','table', 'comment'], function () {
        var comment = layui.comment
            , table = layui.table
            , $ = layui.$
            , form = layui.form;

        //渲染
        comment.Table_Rander({
            elem: '#lanbt_table'
            , url: ApiUrl.QuartzAPI.QueryJobAll.url //数据接口
            , cols: [[ //表头
                { field: 'group', title: '任务组', width: 100 }
                , { field: 'name', title: '任务名称', width: 200 }
                , { field: 'displayState', title: '状态', width: 70 ,templet:function(d){
                        var sty = 'layui-bg-green';
                        if(d.displayState == "正常" || d.displayState == "完成" || d.displayState == "暂停"){
                            sty = 'layui-bg-green';
                        }else if(d.displayState == "异常"){
                            sty = '';
                        }
                        else if(d.displayState == "阻塞" || d.displayState == "不存在" || d.displayState == "未知"){
                            sty = 'layui-bg-orange';
                        }
                        return '<span class="layui-badge '+ sty +'">'+ d.displayState +'</span>'
                    }
                }
                , { field: 'runNumber', title: '触发次数', width: 100,align:'center' }
                , { field: 'jobType', title: '任务类型', width: 100 ,align: 'center', templet: function(d){
                        var html = 'layui-bg-blue';
                        if(d.jobType == "API"){
                            html =  'layui-bg-blue';
                        }else if(d.jobType == "邮箱"){
                            html =  'layui-bg-cyan';
                        }
                        return '<span class="layui-badge '+ html +'">'+ d.jobType +'</span>';
                    }
                }
                , { field: 'lastErrMsg', title: '异常信息', width: 100,align: 'center',templet:function(d){
                        if(d.lastErrMsg){
                            return '<span class="layui-btn layui-badge  power-show" lay-menu-code="QuartzAction:GetException" lay-event="error">查看</span>';
                        }else
                        {
                            return '-'
                        }
                        
                    }
                }
                , { field: 'startTime', title: '开始时间', minWidth: 160, templet: '<div>{{layui.util.toDateString(d.startTime,\'yyyy-MM-dd HH:mm:ss\')}}</div>'  }
                , { field: 'endTime', title: '结束时间', minWidth: 160, templet: '<div>{{layui.util.toDateString(d.endTime,\'yyyy-MM-dd HH:mm:ss\')}}</div>' }
                , { field: 'nextExecutionTime', title: '下次执行时间', minWidth: 160, templet: '<div>{{layui.util.toDateString(d.nextExecutionTime,\'yyyy-MM-dd HH:mm:ss\')}}</div>'  }
                , { field: 'interval', title: '执行计划', width: 130 }
                , { field: 'description', title: '说明', minWidth: 300 }
                , { fixed: 'right', minWidth: 200, title: '操作', align: 'center', toolbar: '#line_tool' }
            ]]
            , toolbar: '#heard_tool'
            , done: function (res, curr, count) {
                comment.CheckPermission();
            }
        });

        //头部工具栏事件
        table.on('toolbar(lanbt_table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'search':
                    var name = $("#s_name").val();
                    var group = $("#s_group").val();
                    table.reload("lanbt_table", {
                        where: {
                            name: name,
                            group: group,
                        }
                        , done: function () {
                            $('#s_name').val(name);
                            $('#s_group').val(group);
                            comment.CheckPermission();
                        }
                        , page: {
                            pageIndex: 1
                        }
                    })
                    break;
                case 'add':
                    layer.open({
                        type: 1
                        , resize: false
                        , title: '添加任务'
                        , content: $('#quartz_operation').html()
                        , shade: 0.8//遮幕
                        , area: ['700px', '600px']//定义宽高
                        , success: function (layero, index) {

                            form.on('submit(save)', function (data) {
                                comment._ACP(ApiUrl.QuartzAPI.AddJob, JSON.stringify(data.field), function (res) {
                                    if (res.success) {
                                        comment.Success(res.result);
                                        setTimeout(function () {
                                            table.reload('lanbt_table');
                                            layer.close(index);
                                        }, 200)
                                    } else {
                                        comment.Error(res.errorMsg);
                                    }
                                })

                            })
                        }
                    })
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(lanbt_table)', function (obj) { //注：tool 是工具条事件名，lanbt_table 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            switch (layEvent) {
                case 'delete':
                    layer.confirm('确定要删除任务【' + data.group + ' .'+data.name+'】吗？', function (index) {
                        var json = { name: data.name, group: data.group };
                        comment._ACP(ApiUrl.QuartzAPI.DeleteJob, JSON.stringify(json), function (res) {
                            if (res.success) {
                                comment.Success(res.result);
                                table.reload("lanbt_table");
                            } else {
                                comment.Error(res.errorMsg)
                            }
                        })
                    })
                    break;
                case 'edit':
                    layer.open({
                        type: 1
                        , resize: false
                        , title: '编辑任务【' + data.group + ' .'+data.name + '】'
                        , content: $('#quartz_operation').html()
                        , shade: 0.8//遮幕
                        , area: ['700px', '600px']//定义宽高
                        , success: function (layero, index) {
                            //获取单个任务信息
                            comment._ACP(ApiUrl.QuartzAPI.GetJobDetail, { name: data.name,group:data.group }, function (res) {
                                if (res.success) {
                                    form.val('form', res.result);
                                    //触发器类型初始化
                                    var trigger_type = $('#triggerType').val();
                                    if (trigger_type == '1') {
                                        $(".lanbt-li-cron").show();
                                        $('.lanbt-li-simple').hide();
                                    }
                                    else if (trigger_type == '2') {
                                        $(".lanbt-li-cron").hide();
                                        $('.lanbt-li-simple').show();
                                    }
                                    else {
                                        $(".lanbt-li-cron").hide();
                                        $('.lanbt-li-simple').hide();
                                    }
                                    //任务类型初始化
                                    var job_type = $('#jobType').val();
                                    if (job_type == '1') {
                                        $(".lanbt-li-api").show();
                                        $(".lanbt-li-mail").hide();
                                    }
                                    else if (job_type == '2') {
                                        $(".lanbt-li-api").hide();
                                        $(".lanbt-li-mail").show();
                                    }
                                    else {
                                        $(".lanbt-li-api").hide();
                                        $(".lanbt-li-mail").hide();
                                    }
                                } else {
                                    comment.Error(res.errorMsg);
                                }
                            })
                            comment.InitTips();
                            //修改提交
                            form.on('submit(save)', function (update) {
                                var params = {
                                    oldName : data.name,
                                    oldGroup : data.group,
                                    jobDetail : update.field
                                };
                                comment._ACP(ApiUrl.QuartzAPI.UpdateJob, JSON.stringify(params), function (res) {
                                    if (res.success) {
                                        comment.Success(res.result);
                                        setTimeout(function () {
                                            table.reload('lanbt_table', null, true);
                                            layer.close(index);
                                        }, 200)
                                    } else {
                                        comment.Error(res.errorMsg);
                                    }
                                })

                            })
                        }
                    })
                    break;
                case 'stop':
                    layer.confirm('确定要暂停任务【' + data.group + ' .'+data.name+'】吗？', function (index) {
                        var json = { name: data.name,group:data.group };
                        comment._ACP(ApiUrl.QuartzAPI.PauseJob, JSON.stringify(json), function (res) {
                            if (res.success) {
                                comment.Success(res.result);
                                table.reload("lanbt_table");
                            } else {
                                comment.Error(res.errorMsg)
                            }
                        })
                    })
                    break;
                case 'recovery':
                    layer.confirm('确定要恢复任务【' + data.group + ' .'+data.name+'】吗？', function (index) {
                        var json = { name: data.name,group:data.group };
                        comment._ACP(ApiUrl.QuartzAPI.ResumeJob, JSON.stringify(json), function (res) {
                            if (res.success) {
                                comment.Success(res.result);
                                table.reload("lanbt_table");
                            } else {
                                comment.Error(res.errorMsg)
                            }
                        })
                    })
                    break;
                case 'readlog':
                    comment._ACP(ApiUrl.QuartzAPI.GetLogList, { name: data.name, group: data.group }, function (res) {
                        if (res.success) {
                            var html = 
                                `<style>
                                    .msgList {
                                        margin: 5px auto;
                                        width: 90%;
                                        display: block;
                                        overflow: hidden;
                                        word-break: break-word;
                                        padding: 10px;
                                        color: green;
                                    }
                                    
                                    .error {
                                        color: red;
                                    }
                                    
                                    .time {
                                        font-size: 15px;
                                        font-weight: 600;
                                    }
                                    
                                </style >
                                `;
                            if (res.result.length > 0) {
                                var result = JSON.parse(res.result);
                                result.forEach(function (value, index) {
                                    html += value;
                                })
                            }
                            layer.open({
                                type: 1,
                                title: '【' + data.group + ' .' + data.name +'】日志记录',
                                shade: 0.8, //遮幕
                                shadeClose: true,
                                content: html,
                                area: ['700px','95%'],
                                })
                        } else {
                            comment.Error(res.errorMsg);
                        }
                    })
                    break;
                case 'error':
                    comment._ACP(ApiUrl.QuartzAPI.GetException, { name: data.name, group: data.group }, function (res) {
                        if (res.success) {
                            var html =
                                `<style>
                                    .max-win {
                                        margin: auto;
                                        width: 90%;
                                        display: block;
                                        overflow: hidden;
                                        word-break: break-word;
                                        padding: 10px;
                                    }

                                    .err-time {
                                        font-size: 15px;
                                        font-weight: 600;
                                    }

                                    .error {
                                        color: red;
                                    }
                                    
                                </style >
                                <div class="max-win" >
                                `;
                            html += res.result + "</div>";
                            layer.open({
                                type: 1,
                                title: '【' + data.group + ' .' + data.name + '】异常信息',
                                shade: 0.8, //遮幕
                                shadeClose: true,
                                content: html,
                                area: ['600px', '89%']
                            })
                        } else {
                            comment.Error(res.errorMsg);
                        }
                    })
                    break;
            }
        });
        
    });